{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 比对运算",
   "id": "ab99bb8f06b47532"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T07:08:47.493126Z",
     "start_time": "2025-11-11T07:08:47.463191Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "\n",
    "a = torch.tensor([2, 1, 3])\n",
    "b = torch.tensor([2, 2, 2])\n",
    "\n",
    "print(a)\n",
    "print(b)\n",
    "\n",
    "print(torch.eq(a, b))\n",
    "print(torch.equal(a, b))\n",
    "\n",
    "print(a >= b)\n"
   ],
   "id": "8a4939afc87c1dc0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([2, 1, 3])\n",
      "tensor([2, 2, 2])\n",
      "tensor([ True, False, False])\n",
      "False\n",
      "tensor([ True, False,  True])\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 排序运算",
   "id": "9cf993bbc8fe2e6d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T07:55:11.483502Z",
     "start_time": "2025-11-11T07:55:11.480072Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 取出维度为1， 最大的2个值\n",
    "a = torch.tensor([\n",
    "                    [2, 4, 3, 1, 5],\n",
    "                    [2, 3, 4, 1, 4]\n",
    "                 ])\n",
    "\n",
    "b = torch.topk(a, k=2, dim=1, largest=True, sorted=False)\n",
    "# print(b)\n",
    "\n",
    "c = torch.kthvalue(a, k=1, dim=0)\n",
    "print(c)"
   ],
   "id": "75a5b4c7e7261173",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.return_types.kthvalue(\n",
      "values=tensor([2, 3, 3, 1, 4]),\n",
      "indices=tensor([0, 1, 0, 0, 1]))\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 是否有界运算，是否NaN运算",
   "id": "558afaf7325bde60"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T07:59:25.687283Z",
     "start_time": "2025-11-11T07:59:25.683843Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = torch.rand(2, 3)\n",
    "\n",
    "print(a)\n",
    "print(a/0)\n",
    "\n",
    "print( torch.isfinite(a) )\n",
    "print( torch.isfinite(a/0) )\n",
    "\n",
    "print( torch.isinf(a/0) )\n",
    "print( torch.isinf(a) )\n",
    "\n"
   ],
   "id": "86a0ace143dc51eb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.9664, 0.9573, 0.3457],\n",
      "        [0.3188, 0.5775, 0.2334]])\n",
      "tensor([[inf, inf, inf],\n",
      "        [inf, inf, inf]])\n",
      "tensor([[True, True, True],\n",
      "        [True, True, True]])\n",
      "tensor([[False, False, False],\n",
      "        [False, False, False]])\n",
      "tensor([[True, True, True],\n",
      "        [True, True, True]])\n",
      "tensor([[False, False, False],\n",
      "        [False, False, False]])\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T07:59:11.682772Z",
     "start_time": "2025-11-11T07:59:11.681357Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "dc81e1f9d4a1a205",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
